package cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bk.controller.admin.lwbook.vo.LwBookRespVO;
import cn.iocoder.yudao.module.bk.controller.admin.lwbookinapply.vo.*;
import cn.iocoder.yudao.module.bk.controller.app.vo.IdEntReq;
import cn.iocoder.yudao.module.bk.dal.dataobject.lwbookinapply.LwBookInApplyDO;
import cn.iocoder.yudao.module.bk.service.lwbookinapply.LwBookInApplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 书籍飘入")
@RestController
@RequestMapping("/bk/lw-book-in-apply")
@Validated
public class LwBookInApplyController {

    @Resource
    private LwBookInApplyService lwBookInApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建书籍飘入")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:create')")
    public CommonResult<Long> createLwBookInApply(@Valid @RequestBody LwBookInApplySaveReqVO createReqVO) {
        return success(lwBookInApplyService.createLwBookInApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新书籍飘入")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:update')")
    public CommonResult<Boolean> updateLwBookInApply(@Valid @RequestBody LwBookInApplySaveReqVO updateReqVO) {
        lwBookInApplyService.updateLwBookInApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除书籍飘入")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:delete')")
    public CommonResult<Boolean> deleteLwBookInApply(@RequestParam("id") Long id) {
        lwBookInApplyService.deleteLwBookInApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得书籍飘入")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:query')")
    public CommonResult<LwBookInApplyRespVO> getLwBookInApply(@RequestParam("id") Long id) {
        LwBookInApplyDO lwBookInApply = lwBookInApplyService.getLwBookInApply(id);
        return success(BeanUtils.toBean(lwBookInApply, LwBookInApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得书籍飘入分页")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:query')")
    public CommonResult<PageResult<LwBookInApplyRespVO>> getLwBookInApplyPage(@Valid LwBookInApplyPageReqVO pageReqVO) {
        PageResult<LwBookInApplyDO> pageResult = lwBookInApplyService.getLwBookInApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LwBookInApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出书籍飘入 Excel")
    @PreAuthorize("@ss.hasPermission('bk:lw-book-in-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLwBookInApplyExcel(@Valid LwBookInApplyPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LwBookInApplyDO> list = lwBookInApplyService.getLwBookInApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "书籍飘入.xls", "数据", LwBookInApplyRespVO.class,
                BeanUtils.toBean(list, LwBookInApplyRespVO.class));
    }

    @PostMapping("/applyBooks")
    @Operation(summary = "申请书籍明细")
    public CommonResult<List<LwBookRespVO>> applyBooks(@Valid @RequestBody IdEntReq req) {
        return lwBookInApplyService.applyBooks(req);
    }

    @PostMapping("/firstExamine")
    @Operation(summary = "飘入初审")
    public CommonResult<?> firstExamine(@Valid @RequestBody InExamineReq req) {
        return lwBookInApplyService.firstExamine(req);
    }

    @PostMapping("/finaleExamine")
    @Operation(summary = "飘入终审")
    public CommonResult<?> finaleExamine(@Valid @RequestBody InExamineReq req) {
        return lwBookInApplyService.finaleExamine(req);
    }

    @PostMapping("/import")
    @Operation(summary = "导入记录")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('system:user:import')")
    public CommonResult<BkImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                    @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<BkImportExcelVO> list = ExcelUtils.read(file, BkImportExcelVO.class);
        return success(lwBookInApplyService.importBkList(list, updateSupport));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入漂入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<BkImportExcelVO> list = Arrays.asList(
                BkImportExcelVO.builder()
                        .username("yunai").idCard("510xxxxxxxx")
                        .submitTime(LocalDateTime.now()).bookName("xxxx")
                        .isbn("9873334123").status("漂出").build()
        );
        // 输出
        ExcelUtils.write(response, "书籍漂入模板.xls", "漂入列表", BkImportExcelVO.class, list);
    }


}
